package com.example.tao_city.spinnertext;


import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.ArrayList;
import java.util.List;



public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener,View.OnClickListener {
    private Spinner sp1;
    private Textd adapter;
    private List<Parent> list;
    private ListView listView;
    private String[] st;
    private AutoCompleteTextView lt;
    private ToggleButton tb1,tb2;
    private Button btn1,btn2,btn3,btn4;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sp1=(Spinner)findViewById(R.id.sp1);
        list=new ArrayList<Parent>();
        listView=(ListView)findViewById(R.id.tv1);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
        list=new ArrayList<Parent>();
        list.add(new Parent(R.drawable.ic_launcher_background,"城市1"));
        list.add(new Parent(R.mipmap.ic_launcher,"城市4"));
        list.add(new Parent(R.mipmap.ic_launcher_round,"城市2"));
        adapter=new Textd(list,this);
        sp1.setAdapter(adapter);
        lt=findViewById(R.id.l2);
        st= new String[]{"北京1", "北京2", "北京3", "上海1", "上海2", "上海3"};
        ArrayAdapter adapter=new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,st);
        lt.setAdapter(adapter);
        lt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String a= String.valueOf(s);
                for(int i=0;i<6;i++){
                    if(st[i].equals(a)){
                        Toast.makeText(MainActivity.this,"选择了："+a,Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        tb1=findViewById(R.id.togglebtn);
        tb2=findViewById(R.id.togglebtn1);
        tb1.setOnCheckedChangeListener(this);
        tb2.setOnCheckedChangeListener(this);
        btn1=findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder abder=new AlertDialog.Builder(MainActivity.this);
                abder.setTitle("单选对话框");
                abder.setSingleChoiceItems(st, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"选择了"+st[which],Toast.LENGTH_SHORT).show();
                    }
                });
                abder.show();
            }
        });
        btn2=findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder abder=new AlertDialog.Builder(MainActivity.this);
                abder.setTitle("单选对话框");
                abder.setMultiChoiceItems(st, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if(isChecked){
                            Toast.makeText(MainActivity.this,"你选择了"+st[which],Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(MainActivity.this,"你放弃了"+st[which],Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                abder.show();
            }
        });
        btn3=findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,GridViewActivity.class);
                startActivity(intent);
            }
        });

        //getTotalHeightofListView(listView);
       // setlistviewheight(listView);
        setListViewHeightBasedOnChildren(listView);
        btn4=findViewById(R.id.btn4);
        btn4.setOnClickListener(this);
    }
    private List<String> getData(){
        List<String> data = new ArrayList<String>();
        data.add("测试数据1");
        data.add("测试数据2");
        data.add("测试数据3");
        data.add("测试数据4");

        return data;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.togglebtn:
                if (isChecked) {
                    Toast.makeText(this, "选择1开", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "选择1关", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.togglebtn1:
                if (isChecked) {
                    Toast.makeText(this, "选择2开", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "选择2关", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    //测量list view的高
    private void setlistviewheight(ListView listView){
        if(listView==null){
            return ;
        }
        SimpleAdapter simpleAdapter= (SimpleAdapter) listView.getAdapter();
        if(simpleAdapter==null){
            return;
        }
        int height=0;
        for(int i=0;i<simpleAdapter.getCount();i++){
            View listitem=simpleAdapter.getView(i,null,listView);
            listitem.measure(0,0);
            height+=listitem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params=listView.getLayoutParams();
        params.height=height+(listView.getDividerHeight()*(simpleAdapter.getCount()-1));
        listView.setLayoutParams(params);
    }
    public void getTotalHeightofListView(ListView listView) {
        ListAdapter mAdapter = listView.getAdapter();
        if (mAdapter == null) {
           return;
        }
        int totalHeight = 0;
        for (int i = 0; i < mAdapter.getCount(); i++) {
            View mView = mAdapter.getView(i, null, listView);
            mView.measure(
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            //mView.measure(0, 0);
            totalHeight += mView.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (mAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
    private void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn4:
                Intent a=new Intent();
                a.setClass(this,ProgressBarActivity.class);
                startActivity(a);
                break;
        }
    }
}
